<?php
namespace app\admin\controller;
use think\Db;
class Log extends Base
{   
    public function lists(){
        if( is_post() ){
            $cur_page     = input('page');
            $type     = input('type');
            $keywords = input('keywords');
            $min_time = input('min_time');
            $max_time = input('max_time');
            $sql = "
                select
                    [1]
                from
                    log l
                left join
                    admin_user au
                on
                    l.action_user_id = au.id
                ";
            $param = [];
            $w = ['where'];

            if ( $type != '100all' ) {
                $w[] = " and l.type = ?";
                $param[] = $type;
            }

            if ( !empty($keywords) ) {
                $w[] = " and (l.msg like ? or l.ip like ?)";
                $param[] = "%{$keywords}%";
                $param[] = "%{$keywords}%";
            }
            if ( !empty($min_time) ) {
                $w[] = " and l.create_time >= ?";
                $param[] = strtotime($min_time);
            }
            if ( !empty($max_time) ) {
                $w[] = " and l.create_time <= ?";
                $param[] = strtotime($max_time);
            }

            #条件判断
            if (count($w)>1){
              $w[1] = str_replace("and","",$w[1]);
              $sql .= implode(" ",$w);
            }
            
            #分页
            $page_size = c('a.page_size');
            $page_html = com_list_page($sql, $param, $cur_page,$page_size);

            #内容
            $start = ($cur_page - 1) * $page_size;
            $field = "l.ip,l.id,l.user_id,l.action_user_id,l.msg,l.create_time,l.type,au.username";
            $sql_list = str_replace("[1]",$field,$sql);
            $sql_list .= " order by l.create_time desc,l.id desc limit {$start},{$page_size}";

            $rslist = Db::query($sql_list, $param);
            foreach ($rslist as $k => $v) {
                $rslist[$k]['type']         = c("type.log_type",$v['type']);
                $rslist[$k]['create_time']  = com_formate_time($v['create_time']);
            }
            $body_html = com_list_body($rslist, [ "id","type", "msg","create_time","ip","username"]);
            com_check(['data'=>['com_body'=>$body_html, 'com_page'=>$page_html]]);
        }

        $data = [
            'type'      => ['100all'=>'全部类型'] + c('type.log_type'),
        ];
        return view('', $data);
    }
}